<template>
  <div>
    <el-row class="row-bg">
      <el-col :span="6">
        <div class="grid-content ep-bg-purple-light"/>
        <el-card class="box-card">
          <template #header>
            <div class="card-header">
              <span>
                <el-input
                    v-model="params.name"
                    placeholder="名称查询"
                    clearable
                >
                <template #append>
                  <el-button :icon="Search" @click="getList"/>
                </template>
             </el-input>
              </span>
              <el-button class="button" type="primary" @click="editOrAdd(null)">新增</el-button>
            </div>
          </template>
          <el-table :data="dataList" ref="tableRef" v-loading="loading">
            <el-table-column prop="username" label="用户名"/>
            <el-table-column prop="name" label="名称"/>
            <el-table-column prop="password" label="密码"/>
            <el-table-column prop="createTime" label="创建时间"/>
            <el-table-column prop="updateTime" label="更新时间"/>
            <el-table-column fixed="right" label="操作" width="220" align="center">
              <template #default="{ row }">
                <el-button type="primary" @click="editOrAdd(row)">修改</el-button>
                <el-popconfirm
                    title="你确定删除吗？"
                    confirm-button-text="确定"
                    cancel-button-text="取消"
                    @confirm="removeById(row)"
                >
                  <template #reference>
                    <el-button type="danger">删除</el-button>
                  </template>
                </el-popconfirm>
              </template>
            </el-table-column>
          </el-table>
          <div style="margin-top: 10px">
            <el-pagination
                v-model:current-page="currentPage"
                v-model:page-size="currentPageSize"
                :page-sizes="[2, 10, 20, 100]"
                layout="sizes, prev, pager, next"
                :total="total"
                @size-change="handleSizeChange"
                @current-change="handleChangePage"
            />
          </div>
        </el-card>
      </el-col>
    </el-row>
    <!--    新增修改框开始-->
    <el-dialog
        v-model="dialogVisible"
        width="30%"
        :title="dialogType ? '新增' : '编辑'"
        :before-close="handleClose"
        :show-close="false"
    >
      <span>
        <el-form :model="tableForm" ref="tableRef" :rules="rules">
        <el-form-item label="姓名" :label-width="80" prop="name">
          <el-input v-model="tableForm.name" placeholder="请输入姓名"/>
        </el-form-item>
        <el-form-item label="用户名" :label-width="80" prop="username">
          <el-input v-model="tableForm.username" placeholder="请输入用户名"/>
        </el-form-item>
        <el-form-item label="密码" :label-width="80" prop="password">
          <el-input type="password" show-password v-model="tableForm.password" placeholder="请输入密码"/>
        </el-form-item>
      </el-form>
      </span>
      <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">返回</el-button>
        <el-button type="primary" @click="submit">
          确定
        </el-button>
      </span>
      </template>
    </el-dialog>
    <!--    新增修改框结束-->
  </div>
</template>

<script setup>
import {page, add, update, remove} from './api/user'
import {reactive, ref} from "vue";
import {Search} from '@element-plus/icons-vue'
import {ElMessage} from "element-plus";

let dataList = ref([]);
let loading = ref(true);
let dialogType = ref(true);
let dialogVisible = ref(false);

let total = ref(0)
let currentPage = ref(1)
let currentPageSize = ref(1)

const rules = reactive({
      name: [
        {required: true, message: '姓名不能为空', trigger: 'blur'}
      ],
      username: [
        {required: true, message: '用户名不能为空', trigger: 'blur'}
      ],
      password: [
        {required: true, message: '密码不能为空', trigger: 'blur'}
      ]
    }
)
let tableForm = ref({
  id:'',
  name: '',
  username: "",
  password: "",
})
let params = reactive({
  name: undefined,
  pageNum: 1,
  pageSize: 20
})
const tableRef = ref()


// 表单提交
const submit = () => {
  tableRef.value.validate((valid, fields) => {
    if (valid) {
      if (!tableForm.value.id) {
        add(tableForm.value).then(({data}) => {
          if (data.code === 200) {
            ElMessage({
              message: data.msg,
              type: 'success',
            })
            getList()
            dialogVisible.value = false
          } else {
            ElMessage({
              message: data.msg,
              type: 'warning',
            })
          }
        })
      } else {
        update(tableForm.value).then(({data}) => {
          if (data.code === 200) {
            ElMessage({
              message: data.msg,
              type: 'success',
            })
            getList()
            dialogVisible.value = false
          } else {
            ElMessage({
              message: data.msg,
              type: 'warning',
            })
          }
        })
      }
    } else {
      ElMessage({
        message: '请填写表单信息',
        type: 'warning',
      })
    }
  })
}
// 修改或新增
const editOrAdd = (row) => {
  console.log(row)
  dialogVisible.value = true
  if (row) {
    dialogType.value = false
    tableForm.value = {...row}
  } else {
    dialogType.value = true
    tableForm.value = {}
  }
}
// 获取数据
const getList = () => {
  page(params).then(({data}) => {
    loading.value = false
    dataList.value = data.data.list
    total.value = data.data.totalRow
    currentPage.value = data.data.pageNumber
    currentPageSize.value = data.data.pageSize
  })
}
const handleChangePage = (val) => {
  params.pageNum = val
  currentPage = val
  getList()
}
const handleSizeChange = (val) => {
  params.pageSize = val
  currentPageSize = val
  getList()
}
const removeById = async (row) => {
  const {data} = await remove({
    id: row.id
  })
  if (data.code === 200) {
    ElMessage({
      message: data.msg,
      type: 'success',
    })
    getList()
  } else {
    ElMessage({
      message: data.msg,
      type: 'warning',
    })
  }
}
const handleClose = () => {
  console.log(11)
}
getList()
</script>

<style scoped>
.el-row {
  margin-bottom: 20px;
}

.el-row:last-child {
  margin-bottom: 0;
}

.el-col {
  border-radius: 4px;
}

.grid-content {
  border-radius: 4px;
  min-height: 36px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.text {
  font-size: 14px;
}

.item {
  margin-bottom: 18px;
}

.box-card {
  width: 1200px;
}
</style>
